//
//  XJSystemMSgViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/2.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSystemMSgViewController.h"

//view
#import "XJSystemMSgCell.h"

//model
#import "XJMsgModel.h"

@interface XJSystemMSgViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJSystemMSgViewController

#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80;
    
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XJSystemMSgCell class]) bundle:nil] forCellReuseIdentifier:@"XJSystemMSgCell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



#pragma mark - 二 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"系统消息";
    
    self.tableView.frame = CGRectMake(0, 0, KWidth, KHeight - kStatusBarAndNavigationBarHeight);
    [self.view addSubview:self.tableView];
    
    [self requestData];
}

#pragma mark -  三 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJSystemMSgCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XJSystemMSgCell class])];
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - 四 网络请求
- (void)requestData {
    
    //1:系统消息  2:订单消息
    [XJHttpTool requestCustomMessageListWithMsgType:@"1" page:@"1" successH:^(id responseObjc) {
        
      
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray *messages_list = responseObjc[@"data"][@"messages_list"];
            for (NSDictionary *dic in messages_list) {
                
                XJMsgModel *model = [XJMsgModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:model];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
